﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L08
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] x = { 2, 5, 1, 56, 8 };
            LinkList<int> linkList = new LinkList<int>();
            for (int i = 0; i < x.Length; i++)
                linkList.InsertAt(x[i], i);
            Console.WriteLine("链表为：");
            for (int i = 0; i < x.Length; i++)
                Console.Write("{0}  ",linkList.GetNode(i).Data );
            Console.WriteLine();
            int a;            
            Console.WriteLine("通过调用索引函数得a={0}", a = linkList[2]);
            linkList.IndexOf(1);
            linkList.InsertAt(2, 9);
            linkList.RemoveAt(3);
            Console.WriteLine("链表为：");
            for (int i = 0; i < x.Length; i++)
                Console.Write("{0}  ", linkList.GetNode(i).Data);
            Console.WriteLine();


            Console.ReadKey ();
        }

        public class LinkNode<T>
        {
            private T data;
            private LinkNode<T> next;

            public T Data { get => data; set => data = value; }
            internal LinkNode<T> Next { get => next; set => next = value; }

            public LinkNode(T data)
            {
                this.data = data;
                next = null;
            }

            public LinkNode(T data, LinkNode<T> next)
            {
                Data = data;
                Next = next;
            }
        }

        public class LinkList<T> : ILinkable<T>
        {
            private LinkNode<T> first;
            private bool isEmpty;
            private int count;

            internal LinkNode<T> First { get => first; set => first = value; }

            public bool IsEmpty
            {
                get
                {
                    if (First == null)
                    {
                        return true;
                    }
                    else
                        return false;
                }
            }

            //链表元素个数
            public int Count
            {
                get
                {
                    int i = 0;
                    LinkNode<T> node = first;
                    while (node.Next != null)
                    {
                        node = node.Next;
                        i++;
                    }
                    return i;
                }
            }

            public LinkNode<T> GetNode(int pos)
            {
                LinkNode<T> node = first;
                for (int i = 0; i < pos; i++)
                {
                    if (node.Next == null)
                    {
                        throw new IndexOutOfRangeException();
                    }
                    node = node.Next;
                }
                return node;
            }

            //返回第I个节点的索引函数
            public T this[int i]
            {
                get
                {
                    return GetNode(i).Data;
                }
            }

            //查找指定元素
            public void IndexOf(T t)
            {
                LinkNode<T> node = first;
                while (! node.Data.Equals(t) && node != null)//查找元素值不符合时，会出现异常
                    node = node.Next;
                if (node != null)
                    Console.WriteLine("元素{0}查找成功!", t);
                else
                    Console.WriteLine("元素{0}查找失败!", t);
            }

            //插入节点
            public void InsertAt(T t, int index)
            {
                LinkNode<T> newNode = new LinkNode<T>(t);
                if (index == 0)
                {
                    newNode.Next = first;
                    first = newNode;
                }
                else
                {
                    LinkNode<T> node =first ;
                    for (int i = 1; i <= index - 1; i++)
                    { node = node.Next; }
                    LinkNode<T> preNode = node;
                    LinkNode<T> postNode = node.Next;
                    preNode.Next = newNode;
                    newNode.Next = postNode;
                }
            }

            //删除节点
            public void RemoveAt(int n)
            {
                T data = default(T);
                if (n == 0)
                {
                    data = first.Data;
                    first = first.Next;
                }
                else
                {
                    LinkNode<T> t = first;
                    for (int i = 1; i <= n - 1; i++)
                    { t = t.Next; }
                    LinkNode<T> preNode = t;
                    LinkNode<T> currentNode = t.Next;
                    data = currentNode.Data;
                    LinkNode<T> postNode = t.Next.Next;
                    preNode.Next = postNode;
                }
            }
        }
    }
}
